package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityIdPhotoPreviewBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class IdPhotoPreviewActivity extends BaseAc<ActivityIdPhotoPreviewBinding> {

    public static Bitmap sBitmap;

    @Override
    protected int onCreate() {
        return R.layout.activity_id_photo_preview;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPreviewSave.setOnClickListener(this);
        mDataBinding.tvReshoot.setOnClickListener(this);
        mDataBinding.tvStartMake.setOnClickListener(this);

        mDataBinding.ivPreviewImg.setImageBitmap(sBitmap);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPreviewSave://保存
                save();
                break;
            case R.id.tvReshoot://重新拍摄
                onBackPressed();
                break;
            case R.id.tvStartMake://开始制作
                CutPhotoActivity.sBitmap = sBitmap;
                startActivity(CutPhotoActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //保存预览
    private void save() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //保存到相册
                        ImageUtils.save2Album(sBitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_success);
                    }
                });
            }
        }, 1000);
    }

}